package com.tai.cobrapp.activity;



import com.tai.cobrapp.R;
import com.tai.cobrapp.model.Usuario;
import com.tai.cobrapp.backend.Backend;
import com.tai.cobrapp.database.DatabaseHelper;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.support.v4.view.ViewPager.LayoutParams;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {

	private final static int NUEVO_USUARIO_CUSTOM_ID = 1;
	//private int user_id = -1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView es un metodo de la actividad que toma como parametro un ID de recurso de layout
		setContentView(R.layout.activity_login);
		
		DatabaseHelper.getDatabaseInstance(this).getWritableDatabase();
		
		// Obtengo el ID de la vista boton para luego setearle el evento click
		final Button newUserButton = (Button)findViewById(R.id.new_user_btn);
		
		newUserButton.setOnClickListener(new Button.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showDialog(NUEVO_USUARIO_CUSTOM_ID);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// To DO: cambiar por un menu personalizado
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	@Override
	protected void onStop() {
		finish();
		super.onStop();
	}
	
	
	public void login_btn_pressed(View view)
	{
		
		EditText usuario = (EditText)this.findViewById(R.id.user_name_editText);
		EditText pass = (EditText)this.findViewById(R.id.user_password_editText);
		
		if(validarLogIn(usuario.getText(), pass.getText())){
			Intent i = new Intent(this, OptionsMenuActivity.class);
			i.putExtra("user_name", usuario.getText().toString());
			startActivity(i);
			usuario.setText("");
			pass.setText("");
		}else{
			usuario.setText("");
			pass.setText("");
			Toast.makeText(LoginActivity.this, R.string.user_or_pass_incorrect, Toast.LENGTH_LONG).show();
		}
		
		
	}
	
	public void retrieve_pass_btn_pressed(View view)
	{
		
		EditText usuario = (EditText)this.findViewById(R.id.user_name_editText);

		if(usuario.getText().toString() == null || usuario.getText().toString().isEmpty()){
			Toast.makeText(LoginActivity.this, R.string.enter_username, Toast.LENGTH_LONG).show();
		}else{
			if(!existeU(usuario.getText())){
				Toast.makeText(LoginActivity.this, R.string.user_doesnt_exist, Toast.LENGTH_LONG).show();
			}else{
				String passAux = Backend.getInstance().obtenerUserPassword(this, usuario.getText());

				Toast.makeText(LoginActivity.this, this.getString(R.string.user_pass_begins) +": "+passAux.substring(0, 1)+"  "+this.getString(R.string.and_ends_with)+": "+passAux.substring(passAux.length()-1, passAux.length()), Toast.LENGTH_LONG).show();
			}
		}
	}
	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialogo;
		
		switch(id){
		case NUEVO_USUARIO_CUSTOM_ID:
			dialogo = new Dialog(this);
			dialogo.setContentView(R.layout.new_user);
			dialogo.setTitle(R.string.new_user);
			
			android.view.WindowManager.LayoutParams params = dialogo.getWindow().getAttributes();
			params.width = LayoutParams.MATCH_PARENT;
			params.height = LayoutParams.MATCH_PARENT;
			dialogo.getWindow().setAttributes(params);
			
			final EditText usuario = (EditText)dialogo.findViewById(R.id.username);
			final EditText pass = (EditText)dialogo.findViewById(R.id.password);
			Button crearUsr = (Button)dialogo.findViewById(R.id.botonCrearUsuario);
			Button cancelarUsr = (Button)dialogo.findViewById(R.id.botonCancelarUsuario);
			
					
			android.view.View.OnClickListener crearListener = new Button.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					
					if(existeU(usuario.getText())){
						Toast.makeText(LoginActivity.this, R.string.user_already_exists, Toast.LENGTH_SHORT).show();
						usuario.setText("");
						pass.setText("");
					}else{
						Usuario user = new Usuario(usuario.getText(), pass.getText());
						//user.setUsername(usuario.getText());
						//user.setPassword(pass.getText());
						insertUsr(user);
						//base.insertarUsuario(user);
						Toast.makeText(LoginActivity.this, R.string.user_creation_successful, Toast.LENGTH_SHORT).show();
						dismissDialog(NUEVO_USUARIO_CUSTOM_ID);
					}					
				}
			};
			
			android.view.View.OnClickListener cancelarListener = new Button.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					dismissDialog(NUEVO_USUARIO_CUSTOM_ID);										
				}
			};
			
			crearUsr.setOnClickListener(crearListener);
			cancelarUsr.setOnClickListener(cancelarListener);
			
			return dialogo;
		
		default:
			return super.onCreateDialog(id);
		}
		
		
	}
	public Boolean validarLogIn(Editable usr, Editable pass){
		Integer user_id = DatabaseHelper.getDatabaseInstance(this).validacionUsuario(usr, pass);
		if(user_id != -1){
			Usuario.getUserInstance().setId(user_id);
			Usuario.getUserInstance().setUsername(usr);
			return true;
		}else{
			return false;
		}
	}
	public boolean existeU(Editable usr){
		//return false;
		if(DatabaseHelper.getDatabaseInstance(this).existeUsuario(usr)){
			return true;
		}else{
			return false;
		}
	}
	public void insertUsr(Usuario u){
		DatabaseHelper.getDatabaseInstance(this).insertarUsuario(u);
	}
}
